import '../utils/tools.dart';
import 'comparable.dart';
import 'serializable.dart';

class MemberModel extends Serializable implements Equable<int> {
  int id = 0;
  String username = '';
  String nickname = '';
  String realname = '';
  int levelId = 0;
  String mobile = '';
  bool mobileBind = false;
  String email = '';
  bool emailBind = false;
  String avatar = '';
  int gender = 0;
  int birth = 0;

  String province = '';
  String city = '';
  String county = '';
  String address = '';

  int isAgent = 0;
  int referer = 0;
  String agentCode = '';

  int credit = 0;
  int money = 0;
  int reward = 0;
  int frozeCredit = 0;
  int frozeMoney = 0;
  int frozeReward = 0;

  MemberModel();

  MemberModel.fromJson(Map userinfo) {
    id = Tools.parseInt(userinfo['id']);
    username = userinfo['username'] ?? '';
    nickname = userinfo['nickname'] ?? '';
    avatar = Tools.fixImageUrl(userinfo['avatar']);
    realname = userinfo['realname'] ?? '';

    levelId = userinfo['level_id'];

    if (userinfo.containsKey('mobile')) {
      mobile = userinfo['mobile'] ?? '';
      mobileBind = Tools.parseBool(userinfo['mobile_bind']);
      email = userinfo['email'] ?? '';
      emailBind = Tools.parseBool(userinfo['email_bind']);
      gender = Tools.parseInt(userinfo['gender']);
      birth = Tools.parseInt(userinfo['birth']);
    }

    if (userinfo.containsKey('province')) {
      province = userinfo['province'] ?? '';
      city = userinfo['city'] ?? '';
      county = userinfo['county'] ?? '';
      address = userinfo['address'] ?? '';
    }

    isAgent = Tools.parseInt(userinfo['is_agent']);
    referer = Tools.parseInt(userinfo['referer']);

    agentCode = userinfo['agentcode'] ?? '';

    if (userinfo.containsKey('money')) {
      credit = Tools.parseInt(userinfo['credit']);
      money = Tools.parseInt(userinfo['money']);
      reward = Tools.parseInt(userinfo['reward']);
      frozeCredit = Tools.parseInt(userinfo['froze_credit']);
      frozeMoney = Tools.parseInt(userinfo['froze_money']);
      frozeReward = Tools.parseInt(userinfo['froze_reward']);
    }
  }

  @override
  int get compareField => id;

  @override
  Map toJson() {
    return {
      'id': id,
      'username': username,
      'nickname': nickname,
      'realname': realname,
      'avatar': avatar,
      'mobile': mobile,
      'email': email,
      'gender': gender,
      'birth': birth,
      'province': province,
      'city': city,
      'county': county,
      'address': address,
    };
  }
}
